package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.iwhalecloud.bss.kite.client.vo.scene.*;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneProductModuleVO;
import com.iwhalecloud.bss.kite.dataservice.cache.ServiceControlScopeCache;
import com.iwhalecloud.bss.kite.dataservice.entity.ServiceControlScope;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.transaction.TransactionContext;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.ztesoft.bss.order.base.consts.IKeyValues;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;



@Service
public class LocalDealProdAttrModuleHandle extends LocalAbstractProductModuleHandler {
    /**
     * 该组件的作用是在点击二次类业务的操作类型时，读取service_control_scope表的配置。
     * 重新加载页面展示属性是否可编辑（不是在业务信息变更模板 而是在原来的模块上面）
     * @author yuan.guofei<br>
     * @param context
     * @param sceneModule
     * @return 返回当前组件构建的对象，后面会将这个对象传给子组件，除非没有子组件，否则不应该返回null
     */

    @Autowired
    private IQuerySceneInstService querySceneInstService;


    @Override
    public List<? extends ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
        SceneNewOfferSeneVO sceneNewOfferSeneVO = (SceneNewOfferSeneVO) context.getSceneVo();
        SceneProductModuleVO sceneProductModuleVO = sceneNewOfferSeneVO.getSceneProductModuleVO();
        List<ProductModuleVO> productModuleVOList = sceneProductModuleVO.getProduct();
        String sceneInstId = context.getSceneVo().getSceneInstId();
        List<String> serviceOfferIdList = querySceneInstService.getExtParameterValue(sceneInstId, "BUSI_CHANGE_SERVICEOFFERID");
        // 获取当前所有勾选的操作类型
        List<KiteProdInst> kiteProdInsts = querySceneInstService.listProdInsts(sceneInstId);
        List<ServiceControlScope> allServiceControlScopeList = new ArrayList<ServiceControlScope>();

        // 没有操作类型时new一个避免下面空指针
        if(KiteListUtils.isEmpty(serviceOfferIdList)) {
            serviceOfferIdList = new ArrayList<String>();
        }
        // 获取所有ServiceControlScope表中的配置
        serviceOfferIdList.forEach(serviceOfferId -> {
            kiteProdInsts.forEach(kiteProdInst -> {
                allServiceControlScopeList.addAll(ServiceControlScopeCache.get(serviceOfferId + SymbolConsts.HYPHEN + kiteProdInst.getProdId()));

            });
        });

        setGroupAttrIseidt(productModuleVOList, allServiceControlScopeList);

        return productModuleVOList;
    }

    public void setGroupAttrIseidt(List<ProductModuleVO> productModuleVOList, List<ServiceControlScope> allServiceControlScopeList) {
        for(ProductModuleVO productModuleVO : productModuleVOList) {
            List<ProdRoleModuleVO> prodRoleList = productModuleVO.getProdRole();
            for (ProdRoleModuleVO prodRoleModuleVO : prodRoleList) {
                // 这里获取产品实例要直接从容器中去获取，后面保存属性的时候也是从容器中去获取的。不然会导致拿到的不是同一个对象。
                KiteProdInst prodInst = TransactionContext.getContainer().getInst(InstType.PROD_INST, prodRoleModuleVO.getProdInstId(), KiteWebContext.getContext().getCustId());
                List<GroupProdAttrModuleVO> prodAttrList =  prodRoleModuleVO.getGroupProdAttr();
                for (GroupProdAttrModuleVO groupProdAttrModuleVO : prodAttrList) {
                    List<ProdAttrVO> prodAttrVOList = groupProdAttrModuleVO.getFormProdAttrList();
                    for (ProdAttrVO prodAttrVO : prodAttrVOList) {
                        // 没有勾选操作类型时都设置为F
                        KiteAttr attr = prodInst.getAttr(prodAttrVO.getFieldName());
                        if (KiteListUtils.isEmpty(allServiceControlScopeList)) {
                            prodAttrVO.setIsEdit(IKeyValues.IFTRUE_F);
                            attr.setIsEdit(IKeyValues.IFTRUE_F);
                        }
                        // 获取ServiceControlScope表中的配置然后以配置为准
                        else {
                            for (ServiceControlScope serviceControlScope : allServiceControlScopeList) {
                                if (KiteStringUtils.equals(prodRoleModuleVO.getProdId(),serviceControlScope.getProductId()) && KiteStringUtils.equals(prodAttrVO.getFieldName(),serviceControlScope.getFieldName())) {
                                    prodAttrVO.setIsEdit(serviceControlScope.getIsEdit());
                                    attr.setIsEdit(serviceControlScope.getIsEdit());
                                }

                            }
                        }

                    }

                }
            }
        }

    }

}
